﻿using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.OfficeArt
{
    internal struct OfficeArtFOPTEOPID
    {

        public bool fBid;
        /*        {
                    get { return (opidab & 0x4000) != 0; }
                }
                */
        public bool fComplex;
        /* {
             get { return (opidab & 0x8000) != 0; }
         }
        */
        public static int ByteLength
        {
            get { return 2; }
        }

        private OpId opid;

        public OfficeArtFOPTEOPID(OpId propertyId, bool isComplex)
        {
            this.opid = propertyId;
            switch(propertyId)
            {
                case OpId.Pib:
                    case OpId.PibName:
                    case OpId.WzName:
                    case OpId.WzDescription:
                    this.fBid = true;
                    break;
                default:
                    fBid = false;
                    break;
            }
            this.fComplex = isComplex;
        }

        public void Read(BinaryReader reader)
        {
            var opidab = reader.ReadUInt16();
            this.fBid = (opidab & 0x4000) != 0;
            this.fComplex = (opidab & 0x8000) != 0;
            this.opid = (OpId) (opidab & 0x3FFF);
            Contract.Assert(OpidAb == opidab);
        }


        public ushort OpidAb
        {
            get
            {
                ushort result = (ushort) opid
                    ;
                if (fBid)
                    result |= 0x4000;
                if (fComplex)
                    result |= 0x8000;
                return result;
            }
        }

        public OpId Opid
        {
            get { return opid; }
        }

        public void Write(BinaryWriter writer)
        {
            writer.Write(this.OpidAb);
        }
    }
}